﻿using System.Collections.Generic;
using aprogblog.App.MetaWeblog;
using aprogblog.Domain;
using CookComputing.XmlRpc;

namespace aprogblog.App.Models {

    public class WeblogService : XmlRpcService, MetaWeblogAPI, BloggerAPI {
    
        Blog blog = ObjectFactory.Blog;
        public Blog Blog {
            get { return blog; }
            set { blog = value; }
        }
        
        public virtual Entry EntryFrom(Post Post) { return Post.ToEntry(); }
        
        public Post GetPost(string PostId, string UserName, string Password) {
            return Blog.FindById(PostId).ToPost();
        }

        public Post[] GetRecentPosts(string Blogid, string UserName, string Password, int NumberOfPosts) { return 
            Blog.RecentEntries(NumberOfPosts).ToPosts()
        ;}        

        public string AddPost(string Blogid, string UserName, string Password, Post Post, bool Publish) {
            var NewEntry = EntryFrom(Post);
            Blog.Add(NewEntry);
            return NewEntry.Id;
        }

        public bool DeletePost(string Key, string PostId, string UserName, string Password, bool Publish) {
            Blog.Delete(PostId);
            return true;
        }

        public bool UpdatePost(string PostId, string UserName, string Password, Post Post, bool Publish) {
            return true;
        }

        public CategoryInfo[] GetCategories(string Blogid, string UserName, string Password) {
            return new CategoryInfo[0];
        }

        public MediaObjectInfo NewMediaObject(string Blogid, string UserName, string Password, MediaObject MediaObject) {
            return new MediaObjectInfo();
        }

        public BlogInfo[] GetUsersBlogs(string Key, string UserName, string Password) {
            return new List<BlogInfo> { new BlogInfo {
                blogid = Settings.Id, 
                blogName = Settings.BlogName, 
                url = Settings.HomePage
            }}.ToArray();
        }

        public UserInfo GetUserInfo(string Key, string UserName, string Password) {
            return new UserInfo {
                userid = Settings.User, 
                url = Settings.HomePage
            };
        }
    }
}